//承运商银行卡管理

interface ICarrierBankCardManagentDetailViewModel {
    /**
     * 银行卡号
     */
    cardNumber: string;
    /**
     * 开户银行
     */
    bankName: string;
    /**
     * 持卡人
     */
    holder: string;
    /**
     * 开户银行启用禁用
     */
    // bankDis:boolean;
}

interface ICarrierBankCardManagentViewModel extends IBaseViewModel<ICarrierBankCardManagentQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<BankListItemResponse>;
    /**
     * 添加视图
     */
    detail: ICarrierBankCardManagentDetailViewModel;
    /**
     * 添加银行卡
     */
    addBankCard: () => void;
    /**
     * 删除银行卡
     */
    deleteBankCard: (id: string) => void;
    /**
     * 获取银行开户行名称
     */
    getBankName: (s: string) => void;
    /**
     * 开户银行启用禁用
     */
    bankDis:boolean;
}

interface ICarrierBankCardManagentQueryTerms {

}

class CarrierBankCardManagentController extends ListController<ICarrierBankCardManagentViewModel, ICarrierBankCardManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<BankListItemResponse>;
    carrierId: string;

    constructor($scope: ICarrierBankCardManagentViewModel, private bankService: IBankService, private $location: ng.ILocationService, private $ngBootbox: BootboxService,private commonService:ICommonService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.carrierId = JSON.parse(window.localStorage.getItem("carrier").toString()).carrierId;
        this.$scope.deleteBankCard = this.showDeleteBankCodeDialog;
        this.$scope.addBankCard = this.addBankCode;
        this.$scope.getBankName = this.commonService.debounce(this.getBankName,500);
        this.$scope.bankDis = true;

        this.$scope.queryTerms = {
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { displayName: "持卡人姓名", field: 'holder', enableColumnMenu: false },
                { displayName: "银行卡号", field: 'cardNumber', minWidth: 300, enableColumnMenu: false },
                { displayName: "开户银行", field: 'bank', enableColumnMenu: false },
                {
                    displayName: "操作",
                    field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: '<a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteBankCard(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        }
        this.query();
    }

    loadData(queryTerms: ICarrierBankCardManagentQueryTerms, skip: number, count: number): void {
        this.bankService.getList("", this.carrierId, "", true, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            this.$scope.gridOptions.data = result.data.data;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    showDeleteBankCodeDialog = (id: string) => {
        this.$ngBootbox.confirm("是否删除该银行卡号？").then(e => {
            this.bankService.delete(id, "", this.carrierId).then(result => {
                this.reload();
            });
        });
    }

    addBankCode = () => {
        this.$ngBootbox.confirm("确定保存该数据吗?").then(e => {
            this.bankService.add(
                this.$scope.detail.bankName,
                this.$scope.detail.cardNumber,
                this.$scope.detail.holder,
                this.carrierId,
                null
            ).then(result => {
                this.clearInput();
                this.reload();
            });
        });
    }

    clearInput(): void {
        this.$scope.detail = {
            cardNumber: "",
            bankName: "",
            holder: "",
            //bankDis:true,
        };
        this.$scope.bankDis = true;
    }

    getBankName = () => {
        let bankLen = 0, cardNumber = this.$scope.detail.cardNumber;
        if (cardNumber == "" || cardNumber == undefined || cardNumber == null) {
            this.$scope.detail.bankName = "";
        } else {
            bankLen = cardNumber.length;
        }
        if (bankLen > 15) {
            this.bankService.getBankName(cardNumber).then(result => {
                if (result == null) {
                     result = "";
                     this.$scope.bankDis = false;
                }
                this.$scope.detail.bankName = result.data;
                this.$scope.bankDis = true;
            },(rej) => {
                this.$scope.detail.bankName = "";
                this.$scope.bankDis = false;
                return;
            });
        }
    }
}

angular.module("tms").controller("carrierBankCardManagentController", ["$scope", "bankService", "$location", "$ngBootbox","commonService", CarrierBankCardManagentController]);